//
//@File    :	12.12_bank.cpp
//@Time    :	2021/09/16 21:49:49
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	using the Queue interface
//

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "12.10_queue.h"

const int MIN_PER_HR=60;

bool newcustomer(double x);

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios_base;
//setting things up
    std::srand(std::time(0));

    cout<<"Case Study:bank of header Automatic Teller\n";
    cout<<"Enter maximum size of queue: ";
    int qs;
    cin>>qs;
    Queue line(qs);

    cout<<"Enter the number of simulation hours: ";
    int hours;
    cin>>hours;
    //simulation will run1 cycle per minute
    long cyclelimit = MIN_PER_HR * hours;

    cout<<"Enter the average number of customers per hour: ";
    double perhour;
    cin>>perhour;
    double min_per_cust;
    min_per_cust = MIN_PER_HR/perhour;

    Item temp;//new customer data
    long turnaways=0;//turn away bu full queue
    long customers=0;//joined the queue
    long served=0;//served during the simulation
    long sum_lime=0;//sumulative line length
    int wait_time=0;//time until autoteller is free
    long line_wait=0;//cumulative time in line
//running the simulation
    for (int  cycle = 0; cycle < cyclelimit; cycle++)
    {
        if (newcustomer(min_per_cust))
        {
            if (line.isfull())
            {
                turnaways++;
            }
            else
            {
                customers++;
                temp.set(cycle);//cycle=time of arrival
                line.enqueue(temp);//add newcomer to line
            }
            
        }
        if (wait_time<=0&&!line.isempty())
        {
            line.dequeue(temp);//attend next customer
            wait_time=temp.ptime();//for wait_time minuutes
            line_wait+=cycle-temp.when();
            served++;
        }
        if (wait_time>0)
        {
        wait_time--;
        }
        sum_lime+=line.queuecount();
    }
//reporting reasult
    if (customers>0)
    {
        cout<<"customer accepted: "<<customers<<endl;
        cout<<"  customer served: "<<served<<endl;
        cout<<"        turnaways: "<<turnaways<<endl;
        cout<<"average queue size: ";
        cout.precision(2);
        cout.setf(ios_base::fixed,ios_base::floatfield);
        cout<<(double) sum_lime/cyclelimit<<endl;
        cout<<" average wait time: "<<(double) line_wait/served<<" minutes\n";
    }
    else
    {
        cout<<"No customers!\n";
    }
    cout<<"Done!\n";
    system("pause");
    return 0;
}
//x=average time,in minutes ,between customers
//return value is true of customer shows up this minute
bool newcustomer(double x)
{
    return (std::rand()*x/RAND_MAX<1);
}